package com.greensam.pixelengine.manager;

import cn.hutool.core.io.FileUtil;
import com.greensam.pixelengine.config.CosClientConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用的文件管理器
 *
 * @author Macro_Ray
 * @since 2025/7/1 17:42
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CosManager {

    private final CosClientConfig cosClientConfig;

    private final COSClient cosClient;

    /**
     * 上传对象
     *
     * @param key  唯一键
     * @param file 文件
     * @author Macro_Ray
     * @since 2025/7/1
     */
    public PutObjectResult putObject(String key, File file) {
        return cosClient.putObject(cosClientConfig.getBucket(), key, file);
    }


    /**
     * 获取对象
     *
     * @param key 唯一键
     * @author Macro_Ray
     * @since 2025/7/1
     */
    public COSObject getObject(String key) {
        return cosClient.getObject(cosClientConfig.getBucket(), key);
    }

    /**
     * 上传对象（附带图片信息）
     *
     * @param key  唯一键
     * @param file 文件
     * @author Macro_Ray
     * @since 2025/7/2
     */
    public PutObjectResult putPictureObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        // 上传图片必须要对图片进行处理（获取基本信息也是一种对图片的处理）
        PicOperations picOperations = new PicOperations();
        // 1 表示返回原图信息
        picOperations.setIsPicInfo(1);

        // 图片压缩，传入 Rules 规则，将图片格式转化为 WEBP
        List<PicOperations.Rule> rules = new ArrayList<>();
        String webpKey = FileUtil.mainName(key) + ".webp";
        PicOperations.Rule rule = new PicOperations.Rule();
        rule.setFileId(webpKey);
        rule.setBucket(cosClientConfig.getBucket());
        rule.setRule("imageMogr2/format/webp");
        rules.add(rule);

        // 缩略图处理，仅对 大小>20kb的图片生成缩略图（图片较小的话缩略图会比压缩图更大）
        if (FileUtil.size(file) > 20 * 1024) {
            PicOperations.Rule thumbnailRule = new PicOperations.Rule();
            thumbnailRule.setFileId(FileUtil.mainName(key) + "_thumbnail." + FileUtil.getSuffix(key));
            thumbnailRule.setBucket(cosClientConfig.getBucket());
            // 缩放规则： /thumbnail/<width>x<Height>>  (如果大于原图宽高，则不处理)
            thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s>", 128, 128));
            rules.add(thumbnailRule);
        }

        // 构造处理参数
        picOperations.setRules(rules);
        putObjectRequest.setPicOperations(picOperations);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        log.info("上传图片成功：{}", file.getName());
        return putObjectResult;
    }

    /**
     * 删除对象
     *
     * @param key 唯一键
     * @author Ma Chengrui
     * @since 2025/9/15
     */
    public void deleteObject(String key) {
        cosClient.deleteObject(cosClientConfig.getBucket(), key);
    }

}
